package edu.cmu.ece.ds.droidchat.preference;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.util.Toolbox;

public class ProfilePicturePreference extends Preference {

    private static final String TAG = "ProfilePicturePreference";

    private ImageView mProfilePictureImageView;

    public ProfilePicturePreference(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public ProfilePicturePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProfilePicturePreference(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        final LayoutInflater layoutInflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final LinearLayout layout = (LinearLayout) layoutInflater.inflate(
                R.layout.preference, parent, false);

        // the title
        TextView title = (TextView) layout.findViewById(android.R.id.title);
        title.setText(getTitle());

        // the summary
        TextView summary = (TextView) layout.findViewById(android.R.id.summary);
        summary.setText(getSummary());

        // The preview
        mProfilePictureImageView = new ImageView(getContext());
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mProfilePictureImageView.setLayoutParams(p);
        mProfilePictureImageView.setMaxHeight(50);
        mProfilePictureImageView.setMaxWidth(50);
        mProfilePictureImageView.setFocusable(false);
        Drawable d = null;//Toolbox.getMyInfo().getProfilePicture();
        if (d != null) {
            mProfilePictureImageView.setImageDrawable(d);
        }

        // the widget frame
        ViewGroup gp = (ViewGroup) layout
                .findViewById(android.R.id.widget_frame);
        gp.addView(mProfilePictureImageView);

        layout.invalidate();
        return layout;
    }

    public ImageView getProfilePictureImageView() {
        return mProfilePictureImageView;
    }

}
